/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import vn.webapp.dao.CoursesDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Courses;
import vn.webapp.model.Coursing;

@Service("coursesService")
public class CoursesServiceImpl implements CoursesService {

    @Autowired
    private CoursesDAO coursesDAO;

    /**
     *
     * @return
     */
    @Override
    public DataPage<Courses> list() {
        DataPage<Courses> dataCourses = new DataPage<>();
        dataCourses.setData(coursesDAO.list());
        return dataCourses;
    }

    @Override
    public Courses viewDetail(String id) {
        return coursesDAO.viewDetail(id);
    }

    @Override
    public Courses checkExistsCode(String code, int id) {
        return coursesDAO.checkExistsCode(code, id);
    }

    @Override
    public int updateTheCourse(String code,String name, int id, int credits) {
        return coursesDAO.updateTheCourse(code, name, id, credits);
    }

    @Override
    public int removeCourse(int id) {
        return coursesDAO.removeCourse(id);
    }
    
    @Override
    public int saveACourse(String code, String name, int credits)
    {
        Courses course = new Courses();
        course.setCode(code);
        course.setName(name);
        course.setCredits(credits);
        return coursesDAO.saveACourse(course);
    }
    
    @Override
    public DataPage<Courses> filter(int page, int pageSize) {
        DataPage<Courses> dataCourses = new DataPage<>();
        dataCourses.setPageIndex(page);
        dataCourses.setPageSize(pageSize); 
        dataCourses.setDataCount(coursesDAO.count());
        dataCourses.setPageCount(dataCourses.getDataCount()/pageSize);
        if(dataCourses.getDataCount()%pageSize > 0){
            dataCourses.setPageCount(dataCourses.getPageCount()+1);
        }
        dataCourses.setData(coursesDAO.filter(page, pageSize));
        return dataCourses;
    }
    
    @Override
    public Set<Courses> getById(int courseId)
    {
        List<Courses> courses = coursesDAO.getById(courseId);
        Set<Courses> course = new HashSet<Courses>(courses);
        return course;
    }
}
